var express = require('express');
var router = express.Router();
var { checkUsername } = require("../utils")
var { UserModel, MvModel, Album, Picture } = require("../utils/model")

/* GET home page. */
router.get('/', function (req, res, next) {
  console.log(req.session);
  res.render('index', {
    title: '超管系统',
    username: req.session.username,
  });
});

router.get('/login', (req, res) => {
  res.render('login', { username: req.session.username })
})

router.get('/register', (req, res) => {
  res.render('register', { username: req.session.username })
})


// 电影中心
router.get('/movie', (req, res) => {
  checkUsername(req, res, (username) => {
    // 查询电影的数据  
    MvModel.find({}, {
      _id: 0
    })
      .then(result => {
        console.log(result)
        res.render("movie", {
          username,
          result
        })
      })
  })
})

// 电影年排序
router.get('/movie/year', (req, res) => {
  checkUsername(req, res, (username) => {
    var key = req.query.key || "";
    var sortnum = req.query.sortnum;
    // 查询电影的数据  
    MvModel.find({
      $or: [
        { title: new RegExp(key) },
        { genres: new RegExp(key) }
      ]
    }, {
      _id: 0
    })
      .sort({ year: sortnum })
      .then(result => {
        if (result) {
          res.json({
            msg: "已排序",
            code: 200,
            result,
            type: 1
          })
        } else {
          res.json({
            msg: "排序失败",
            code: 200,
            type: 0
          })
        }
      })
  })
})

//电影评分排序
router.get('/movie/average', (req, res) => {
  checkUsername(req, res, (username) => {
    var key = req.query.key || "";
    var sortnum = req.query.sortnum;
    // 查询电影的数据  
    MvModel.find({
      $or: [
        { title: new RegExp(key) },
        { genres: new RegExp(key) }
      ]
    }, {
      _id: 0
    })
      .sort({ "rating.average": sortnum })
      .then(result => {
        if (result) {
          res.json({
            msg: "已排序",
            code: 200,
            result,
            type: 1
          })
        } else {
          res.json({
            msg: "排序失败",
            code: 200,
            type: 0
          })
        }
      })
  })
})


//查询
router.get('/movie/sousuo', (req, res) => {
  checkUsername(req, res, (username) => {
    var key = req.query.key;
    // 查询电影的数据  
    MvModel.find({
      $or: [
        { title: new RegExp(key) },
        { genres: new RegExp(key) },
        { "rating['average']": new RegExp(key) },
        { year: key },
      ]
    },
      {
        _id: 0
      })
      .then(result => {
        if (result) {
          res.json({
            msg: "搜索成功",
            code: 200,
            result,
            type: 1
          })
        } else {
          res.json({
            msg: "搜索失败",
            code: 200,
            type: 0
          })
        }
      })
  })
})

router.get("/mvdetail", (req, res) => {
  checkUsername(req, res, username => {
    var { mid } = req.query;
    MvModel.findOne({
      id: mid
    }).then(result => {
      res.render("detail", {
        username,
        result  // 这条电影的详情数据 
      })
    })

  })
})

// 聊天中心
router.get('/client', (req, res) => {
  checkUsername(req, res, (username) => {
    res.render('client', { username: req.session.username })
  })
})


//我的相册
router.get('/album', (req, res) => {
  checkUsername(req, res, (username) => {
    Album.find().then(result => {
      res.render("myalbum", {
        username,
        result
      })
    })
  })
})

//添加相册
router.post('/addalbum', (req, res) => {
  checkUsername(req, res, (username) => {
    var body = req.body;
    Album.findOne({
      albumname: body.albumname,
    }).then(data => {
      if (data) {
        res.json({
          msg: "相册已存在",
          code: 200,
          type: 0,
        })
      } else {
        body.username = username;
        body.time = new Date();
        body.picturenum = 0;
        Album.insertMany(body)
          .then(result => {
            res.json({
              msg: "新建相册成功",
              code: 200,
              type: 1,
            })
          })
          .catch(err => {
            res.json({
              msg: "新建相册失败",
              code: 200,
              type: 0,
            })
          })
      }
    })
  })
})

//图片页
router.get('/picture', (req, res) => {
  checkUsername(req, res, (username) => {
    var query = req.query
    Picture.find({
      aid: query.aid
    }).then(result => {
      res.render("picture", {
        username,
        result
      })
    })
  })
})

router.post('/upload', (req, res) => {
  checkUsername(req, res, (username) => {
    var body = req.body;
    body.time = new Date();
    Picture.insertMany(body)
      .then(data => {
        Album.find({ _id: body.aid })
          .then(result => {
            console.log(result)
            var num = result[0].picturenum * 1 + 1;
            console.log(num);
            Album.update({ _id: body.aid }, { $set: { picturenum: num } })
              .then(success => {
                res.json({
                  msg: "上传图片相成功",
                  code: 200,
                  type: 1,
                })
              })
          })

      })
      .catch(err => {
        res.json({
          msg: "上传图片相失败",
          code: 200,
          type: 0,
        })
      })
  })
})



module.exports = router;
